package com.sky.task;

import com.sky.constant.MessageConstant;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
@Slf4j
public class OrderTask {

    @Autowired
    private OrderMapper orderMapper;

    //处理一直没有支付的订单
    @Scheduled(cron = "0 * * * * ? ")
    public void prcodeTimeoutOrder(){
        log.info("定时处理超时订单"+LocalDateTime.now());
        List<Orders> orders = orderMapper.getbyStatusOrderTime(Orders.UN_PAID, LocalDateTime.now().plusMinutes(-15));
        if(orders!=null&orders.size()!=0){
            for (Orders order : orders) {
                order.setStatus(Orders.CANCELLED);//直接设置状态取消订单
                order.setCancelReason("订单超时");
                order.setCancelTime(LocalDateTime.now());
                orderMapper.update(order);
            }
        }
    }
    //处理一直在派送的
    @Scheduled(cron = "0 0 1 * * ?")
    public void prcodeDelieryOrder(){
        log.info("处理一直派送中的订单"+LocalDateTime.now());
        //设置一个小时以内的任务，也就是0点时候都任务直接完成
        List<Orders> orders = orderMapper.getbyStatusOrderTime(Orders.DELIVERY_IN_PROGRESS, LocalDateTime.now().plusMinutes(-60));
        if(orders!=null&orders.size()!=0){
            for (Orders order : orders) {
                order.setStatus(Orders.COMPLETED);//直接设置状态完成订单
                orderMapper.update(order);
            }
        }
    }
}
